package com.smart.campus.web.controller.student;

import cn.hutool.crypto.SecureUtil;
import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.core.page.TableDataInfo;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.common.utils.StringUtils;
import com.smart.campus.framework.util.QiNiuFileOptUtil;
import com.smart.campus.framework.util.ShiroUtils;
import com.smart.campus.school.domain.Msg;
import com.smart.campus.school.domain.Student;
import com.smart.campus.school.service.*;
import com.smart.campus.system.domain.SysUser;
import com.smart.campus.web.utils.StudentUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 学生信息Controller
 * 
 * @author Zhihe.Li
 * @date 2020-05-18
 */
@Controller
@RequestMapping("/student/manager")
public class StudentManagerController extends BaseController
{
    private String prefix = "student/manager";

    @Autowired
    private IStudentService studentService;
    @Autowired
    private IMsgService msgService;

    /**
     * 学生个人信息
     * @param modelMap
     * @return
     */
    @GetMapping("/profile")
    public String profile(ModelMap modelMap){

        Student student = StudentUtil.getStudentInfo();
        modelMap.put("student",student);
        return prefix+"/profile";
    }

    @GetMapping("/checkPassword")
    @ResponseBody
    public boolean checkPassword(String password)
    {
        Student student = StudentUtil.getStudentInfo();
        if (matches(student, password))
        {
            return true;
        }
        return false;
    }

    @GetMapping("/profile/avatar")
    public String avatar(ModelMap mmap)
    {
        Student student = StudentUtil.getStudentInfo();
        mmap.put("student", student);
        return prefix + "/avatar";
    }

    /**
     * 保存头像
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAvatar")
    @ResponseBody
    public AjaxResult updateAvatar(@RequestParam("avatarfile") MultipartFile file)
    {
        Student student = StudentUtil.getStudentInfo();
        try
        {
            if (!file.isEmpty())
            {
                //String avatar = FileUploadUtils.upload(Global.getAvatarPath(), file);
                student.setImg(QiNiuFileOptUtil.URL+QiNiuFileOptUtil.upload(file).key);
                if (studentService.updateStudent(student) > 0)
                {
                    StudentUtil.updateStudentSession(student);
                    return success();
                }
            }
            return error();
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
    @GetMapping("/resetPwd")
    public String resetPwd(ModelMap mmap)
    {
        Student student = StudentUtil.getStudentInfo();
        mmap.put("student", student);
        return prefix + "/resetPwd";
    }

    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwd(String oldPassword, String newPassword)
    {
        Student student = StudentUtil.getStudentInfo();
        if (StringUtils.isNotEmpty(newPassword) && matches(student, oldPassword))
        {
            student.setPassword(SecureUtil.md5(newPassword));

            if (studentService.updateStudent(student) > 0)
            {
                StudentUtil.updateStudentSession(student);
                return success();
            }
            return error();
        }
        else
        {
            return error("修改密码失败，旧密码错误");
        }
    }

    // 消息中心
    @GetMapping("/msg")
    public String msg(ModelMap mmap)
    {
        return "student/msg/msg";
    }

    /**
     * 查询消息中心列表
     */
    @PostMapping("/msg/list")
    @ResponseBody
    public TableDataInfo list()
    {
        startPage();
        //加载消息中心
        Msg msg = new Msg();
        msg.setUserId(StudentUtil.getStudentId());
        msg.setUserType(2);
        List<Msg> msgList = msgService.selectMsgList(msg);
        return getDataTable(msgList);
    }
    private boolean matches(Student student, String newPassword)
    {
        return student.getPassword().equals(SecureUtil.md5(newPassword));
    }

    @PostMapping("/updateStudent")
    @ResponseBody
    public AjaxResult updateStudent(Student student)
    {

        if (studentService.updateStudent(student) > 0)
        {
            StudentUtil.updateStudentSession(student);
            return success();
        }
        return error();
    }

    public static void main(String[] args) {
        System.out.println((SecureUtil.md5("123456")));
    }

}
